<template>
  <div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" @change="onChange">
      <van-swipe-item v-for="(item,index) in image" :key="index">
        <div class="img">
          <van-image :src="require('../../assets/img/shareBg.png')" fit='cover' class="bg" />
          <van-image :src="`${resourceURL}${item.url}`" fit='cover' />
        </div>
      </van-swipe-item>

      <template #indicator v-if="image.length!=1">
        <div class="custom-indicator">{{ current + 1 }}/{{image.length}}</div>
      </template>
    </van-swipe>
    <div class="contentBox">
      <div class="top">
        <div class="left">
          <div class="sale_price">{{info.sale_price}}</div>
          <div class="price">{{info.price}}</div>
        </div>
        <div class="content">
          <div class="img">
            <van-image :src="require('../../assets/img/Housekeeping/clock.png')" fit="cover" />
          </div> <span>{{info.content}}</span>
        </div>
      </div>
      <div class="bottom">
        <div class="name">{{info.service_name}}</div>
        <div class="sale">月销{{info.all_num}}</div>
      </div>
    </div>
    <div class="shop" @click="$router.push({
      name:'HousekeepingShop',
      query:{
         shop_id:info.shop_id
      }
    })">
      <van-cell is-link :clickable="false">

        <template #title>
          <van-image :src="`${resourceURL}${shopInfo.imagesurl}`" fit='cover' round />
          <div class="info">
            <div class="name">{{ shopInfo.shop_name}}</div>
            <div class="phone">商家电话：{{shopInfo.shop_phone}}</div>
          </div>
        </template>
      </van-cell>
      <div class="address">
        <van-image :src="require('../../assets/img/my/address.png')" fit="cover" />
        <div class="detail">{{shopInfo.province}}{{shopInfo.city}}{{shopInfo.area}}{{shopInfo.street}}{{shopInfo.address}}</div>
      </div>
    </div>
    <van-cell-group inset>
      <van-cell class="commit_count" :title="`评价(${commit.length})`" value="查看全部" :border="false" is-link center :to="{name:'commitMore',query:{id}}" />
      <van-cell class="commit_item" v-if="commit[0]">
        <template #icon>
          <van-image class="usercover" :src="`${resourceURL}${commit[0].avatar}`" fit="cover" />
        </template>
        <template #title>
          <div class="username">
            <span>{{commit[0].nick_name}}</span>
            <span>{{formatCreateTime(commit[0].add_time)}}</span>
          </div>
          <div class="content">{{commit[0].content}}</div>
          <div class="evaluate_pic">
            <van-image v-for="(item, index) in commit[0].images" :key="index" :src="`${resourceURL}${item}`" fit="cover" @click="imagePreviews(commit[0].images,index)" />
          </div>
        </template>
      </van-cell>
      <van-empty v-else class="empty" description="暂无评价" />
    </van-cell-group>
    <van-cell-group class="describe" inset>
      <div class="title_bg">商品详情</div>
      <div class="richtext" v-html="info?.summary"></div>
    </van-cell-group>
    <van-goods-action>
      <div class="left" @click="saveImage">
        <van-image :src="require('../../assets/img/share.png')" fit="cover" />
        <div class="text">分享</div>
      </div>

      <van-goods-action-button type="danger" text="立即预约" @click="jump" />
    </van-goods-action>
    <van-overlay :show="showShare" @click="showShare = false">

      <img id="poster" class="poster" />
    </van-overlay>

    <div class="shareBox" ref="element">
      <div class="round">

        <div class="img">
          <van-image :src="require('../../assets/img/shareBg.png')" fit='fill' class="bg" />
          <van-image :src="`${resourceURL}/${image[0]?.url}`" fit="cover" />

        </div>

      </div>
      <div class="top">
        <div class="price"><span style=" font-size: 12px;">￥</span>{{ info.sale_price}}</div>
        <div class="name">{{ info.service_name}}</div>
        <div class="codeBox" ref="qrCode"></div>
      </div>
      <van-button type="primary" round :icon="require('../../assets/img/微信.png')" color="#ff7733">长按进行保存</van-button>
      <div class="store">
        <van-image :src="require('../../assets/img/store.png')" fit="cover" />
        <div class="shop_name">{{ shopInfo.shop_name }}</div>
      </div>
    </div>
  </div>
</template>
<script>
import QRCode from 'qrcodejs2'
import html2canvas from "html2canvas"
export default {
  name: '',
  components: {

  },
  mixins: [],
  props: {

  },
  data () {
    return {
      info: {},
      current: 0,
      showShare: false,
      haveCode: false,
      image: [],
      shopInfo: "",
      commit: '',
      id: "",
    }
  },
  computed: {

  },
  watch: {

  },
  mounted () {

  },
  created () {
    this.serviceinfo()

  },
  methods: {
    // 保存图片
    saveImage () {

      if (!this.haveCode) {

        let qrcode = new QRCode(this.$refs.qrCode, {
          text: 'Hellow QRCode!',
          width: (window.innerWidth) * (84 / 375),
          height: (window.innerWidth) * (84 / 375),
          colorDark: "black", // 二维码颜色
          colorLight: "#ffffff", // 二维码背景色
          correctLevel: QRCode.CorrectLevel.H // 容错率,L:容错率为 7%;M:容错率为 15%;Q:容错率为 25%;H:容错率为 30%. PS:容错级别越高抗残破或遮挡的能力就越强
        });
        qrcode.makeCode(`${window.location.href}&from_uid=${this.$store.state.userInfo.user_id}`)


        const img1 = document.querySelector('#poster')
        html2canvas(this.$refs.element, {
          useCORS: true,
        }).then(canvas => {
          const dataURL = canvas.toDataURL("image/png");
          img1.src = dataURL

        })
        this.haveCode = true
      }
      this.showShare = true

    },
    formatCreateTime (val) {
      return this.$moment(val).calendar(null, {
        sameDay: "MM月DD日 [今天] HH:mm:ss",
        nextDay: "MM月DD日 [明天] HH:mm:ss",
        nextWeek: "MM月DD日  HH:mm:ss",
        lastDay: "MM月DD日 [昨天] HH:mm:ss",
        lastWeek: "MM月DD日  HH:mm:ss",
      });
    },
    onChange (index) {
      this.current = index;
    },
    // 获取商品评价
    getProductCommit () {
      this.$axios.get(this.$api.getproductcommit, {
        params: {
          type: 3,
          product_id: this.info.service_id
        }
      }).then(res => {
        this.commit = res.data.data.data

        this.id = this.info.service_id
      })
    },
    // 获取店铺详情
    shopinfo () {
      this.$axios.get(this.$api.shopinfo, {
        params: {
          shop_id: this.info.shop_id,

        }
      }).then((res) => {
        this.shopInfo = res.data.data

      })
    },
    serviceinfo () {
      this.$axios
        .post(this.$api.serviceinfo, {
          service_id: this.$route.query.id
        })
        .then((res) => {
          this.info = res.data.data.service
          this.shopinfo()
          this.getProductCommit()
          this.$nextTick(() => {
            if (res.data.data.image.length) {
              this.image = res.data.data.image
            } else {
              this.image = [{ url: this.info.top_pic }]
            }
            console.log(this.image);
          })





        });
    },

    //跳转至结算页
    jump () {

      this.$router.push({
        name: 'payment',
      })
      localStorage.setItem('HousekeepingDetail', JSON.stringify(this.info))
    }

  }
};
</script>
<style lang='less' scoped>
.shop {
  width: 343px;

  background: #ffffff;
  border-radius: 6px;

  margin: 12px 16px;
  margin-top: 60px;
  box-sizing: border-box;
  .van-image {
    width: 48px;
    height: 48px;
  }
  .van-cell {
    display: flex;
    align-items: center;
    .van-cell__title {
      display: flex;
      .name {
        font-size: 16px;
        font-family: Microsoft YaHei-Bold, Microsoft YaHei;
        font-weight: bold;
        color: #06021a;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .phone {
        font-size: 12px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #202327;
      }
    }
  }
  .address {
    margin: 0 auto;

    width: 319px;
    border-top: 1px solid #f2f2f2;
    display: flex;
    align-items: center;
    font-size: 12px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #202327;
    padding: 6px 0;
    .van-image {
      width: 11px;
      height: 14px;
      margin-right: 10px;
    }
    .detail {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
.my-swipe {
  width: 375px;
  height: 300px;

  .bg {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    width: 375px;
    height: 300px;
  }
  .img {
    .van-image {
      width: 375px;
      height: 300px;
    }
  }
}
.custom-indicator {
  position: absolute;
  right: 16px;
  bottom: 33px;

  font-size: 12px;
  width: 42px;
  height: 20px;
  background: rgba(32, 35, 39, 0.5);
  border-radius: 14px 14px 14px 14px;
  text-align: center;
  line-height: 20px;
}
.contentBox {
  width: 343px;
  height: 87px;
  margin: 0 auto;
  position: absolute;
  top: 260px;
  left: 50%;
  margin-left: -171.5px;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  box-sizing: border-box;
  background: #ffffff;
  border-radius: 6px 6px 6px 6px;
  opacity: 1;
  border: 1px solid #f2f2f2;
  padding: 12px;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 20px;
    .left {
      display: flex;
      align-items: flex-end;

      .sale_price {
        font-size: 20px;
        margin-right: 6px;
        font-weight: bold;
        color: #f33224;
        line-height: 20px;
      }
      .price {
        // position: absolute;
        // top: 7px;
        // left: 80px;
        font-size: 12px;
        color: #202327;
        text-decoration: line-through;
      }
    }
    .content {
      display: flex;
      align-items: center;
      font-size: 12px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: rgba(32, 35, 39, 0.4);
      .img {
        display: flex;
        align-items: center;
        margin-right: 6px;
        height: 16px;
      }
      /deep/ .van-image {
        width: 11px;
        height: 11px;
      }
    }
  }
  .bottom {
    display: flex;
    justify-content: space-between;
    .name {
      font-size: 16px;
      font-family: Microsoft YaHei-Bold, Microsoft YaHei;
      font-weight: bold;
      color: #202327;
    }
    .sale {
      font-size: 14px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #202327;
    }
  }
}
.describe {
  min-height: 300px;

  margin-bottom: 60px;
  .title_bg {
    height: 53px;
    font-size: 16px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #201d1d;
    line-height: 53px;
    //text-align: center;
    text-indent: 0.75em;
    //background: url('../../assets/img/product/product-detail.png') no-repeat;
    // background-size: contain;
    // background-position: 100% 100%;
  }
  .richtext {
    /deep/p {
      padding: 0;
      margin: 0;
      line-height: 26px;
      word-break: break-all;
      word-wrap: break-word;
    }
    /deep/ img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
}
button {
  background: linear-gradient(to right, #ff7733, #ff7733);
  margin-right: 10px;
}

.van-cell-group--inset {
  margin-top: 12px;
}
.product_name {
  color: #470607;
  font-size: 16px;
  font-weight: bold;
  padding: 16px 16px 14px;
  word-break: break-all;
  white-space: pre-wrap;
}
.options {
  display: flex;
  justify-content: space-between;
  padding: 0;
  box-sizing: border-box;
  .star {
    color: #757472;
    background: transparent !important;
    border-color: transparent !important;
  }
  .is-star {
    color: #f9c254;
  }
  .share {
    color: #f9c254;
    background: transparent !important;
    border-color: transparent !important;
  }
  /deep/.van-button {
    flex: 1;
    .van-button__content {
      align-items: center;
      font-size: 16px;
    }
  }
}
.attribute {
  .van-cell {
    padding: 12px 16px;
  }
  /deep/.van-cell__title {
    color: #757472;
  }
  /deep/.van-cell__value {
    color: #bab9b8;
  }
  .express_fee {
    display: flex;
    justify-content: flex-end;
  }
  .fee {
    font-size: 14px;

    font-weight: 400;
    color: #202327;
  }
}

.commit_count {
  .van-cell__title {
    font-size: 14px;
  }
  .van-cell__value {
    font-size: 12px;
  }
}
.commit_item {
  .usercover {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
    overflow: hidden;
  }
  .username {
    display: flex;
    justify-content: space-between;
    color: #909090;
    span:nth-child(1) {
      max-width: 150px;
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    span:nth-child(2) {
      font-size: 12px;
    }
  }
  .content {
    margin-top: 5px;
  }
  .evaluate_pic {
    display: flex;

    .van-image {
      width: 84px;
      height: 84px;
      border-radius: 10px;
      margin-top: 8px;
      overflow: hidden;
      margin-left: 6px;
    }
  }
}
.empty {
  padding: 0 0 10px;
  /deep/ .van-empty__image {
    width: 106px;
    height: 106px;
  }
}
.van-goods-action {
  .left {
    width: 40px;
    margin-left: 15px;
    display: flex;
    align-items: center;
    flex-direction: column;
    .van-image {
      width: 20px;
      height: 20px;
    }
    .text {
      margin-top: 3px;
      font-size: 12px;

      color: #202327;
    }
  }
}
.shareBox {
  position: absolute;
  top: 0;
  z-index: -99;
  width: 294px;

  background: #ffffff;
  border-radius: 6px;
  border: 2px solid #f33224;
  overflow: hidden;

  .round {
    width: 700px;
    height: 600px;
    border-radius: 50%;
    position: absolute;
    top: -94.5%;
    left: -66.5%;
    overflow: hidden;
    .img {
      z-index: 2;
      position: absolute;

      top: 59%;
      left: 28%;
      .van-image {
        width: 294px;
        height: 290px;
      }
    }
    .bg {
      position: absolute;
      z-index: 3;
      top: 3px;
      left: -1px;
      width: 295px !important;
      height: 290px;
      // .van-image {
      //   width: 300px !important;
      //   height: 290px;
      // }
    }
  }
  .top {
    margin-top: 240px;
    margin-left: 11px;
    position: relative;
    z-index: 99;
    .price {
      font-size: 20px;
      font-weight: bold;
      color: #f33224;
    }
    .name {
      font-size: 16px;
      font-weight: bold;
      color: #202327;
    }
    .codeBox {
      width: 84px;
      height: 84px;
      background: #000;
      position: absolute;
      top: -125%;
      left: 68%;
    }
  }
  .van-button {
    margin-left: 11px;
    margin-top: 12px;
    width: 270px;
    height: 44px;
    // background: #ff7733;
    // border: 1px soli;
  }
  .store {
    margin: 11px 0;
    display: flex;
    align-items: center;
    margin-left: 11px;
    .van-image {
      width: 17px;
      height: 17px;
    }
    .shop_name {
      margin-left: 6px;
      font-size: 12px;

      color: #06021a;
    }
  }
}
.van-overlay {
  display: flex;
  justify-content: center;
  align-items: center;
  #poster {
    width: 294px;
    height: 376px;
  }
}
</style>